package com.kaohe.dao;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.kaohe.domain.User;
import com.kaohe.utils.C3P0Utils;

public class LoginDao  {
	
	//登录
	public User login(String username, String password) {
		
		QueryRunner qr=new QueryRunner(C3P0Utils.getDataSource());
		
		String sql="select *from user where username=? and password=? and manager=?";
		
		Object[] param={username,password,1};
		
		User user=null;
		try {
			user = qr.query(sql, param, new BeanHandler<>(User.class));
		} catch (SQLException e) {
 			e.printStackTrace();
		}
		
 		return user;
	}

	//查询总条数
	public int uCount() {
		QueryRunner qr=new QueryRunner(C3P0Utils.getDataSource());
		
		String sql="select count(*) from user";
		Long count=0L;
		try {
			count = (Long) qr.query(sql, new ScalarHandler());
		} catch (SQLException e) {
 			e.printStackTrace();
		}
		return count.intValue();
	}

	//查询当前页数据
	public List<User> uquery(int startIndex, int pageSize) {
 
		QueryRunner qr=new QueryRunner(C3P0Utils.getDataSource());
		
		String sql="select * from user limit ?,?";
		Object[] param={startIndex,pageSize};

		List<User> result=null;
		try {
			result = qr.query(sql, param, new BeanListHandler<>(User.class));
		} catch (SQLException e) {
 			e.printStackTrace();
		}
		
		return result;
	}
	//删除用户信息
	public void deUser(String uid) {
		QueryRunner qr=new QueryRunner(C3P0Utils.getDataSource());
		
		String sql="delete from user where uid=?";
		
		try {
			qr.update(sql, uid);
		} catch (SQLException e) {
 			e.printStackTrace();
		}
	}

}

